// pages/plan/joinPlan.js
Page({
   
  /**
   * 页面的初始数据
   */
  data: {
    active:'',
    imgPath:'/images',
    cardOnsale:null,
    channel:'',
    is_share:'',
    nickname:'',
    head_image:'',
    code:'',
    hash:'lhbb',
    baseUrl: '',
    token:'',
    renew:''   //是否立即续费还是立即加入
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   
    let renew = options.renew;
    let channel =  options.channel;
    let is_share = options.is_share;
    let is_channel = options.hasOwnProperty('channel');
    let is_Share = options.hasOwnProperty('is_share');
    const that=this;
    const util=require("../../utils/util")
    this.setData({
      imgPath:util.getImgPath(),
      channel:is_channel?channel:'',
      is_share:is_Share?is_share:'',
      renew:renew?renew:'',
    })
    if(is_channel){
      wx.setStorageSync('channel', channel)
    }
    if(is_Share){
      wx.setStorageSync('is_Share', is_share)
    }
    //当前在售成长卡
    const api=require("../../api/api");
    // api.user.home({
    //   success(resHome) {
    //     // console.log("home,resHome", resHome)
    //   }
    // });
    // let is_share = wx.getStorageSync('is_share');
    // let channel = wx.getStorageSync('channel');
    // api.card.cardOnsale({
    //   // is_share: is_share?is_share:'',
    //   // channel: channel?channel:'',
    //   success(res){
      
    //   }
    // })
    let token = wx.getStorageSync('token');
    if(!token){
      wx.redirectTo({
        // url: '/pages/plan/joinPlan?channel='+options.channel+'&is_share='+options.is_share
        url: '/pages/index/index?query=fengxiang&channel='+this.data.channel+'&is_share='+this.data.is_share
       })
       return;
    }
    // if(!token){
    //   wx.login({
    //     success: (res) => {
    //       let code = res.code;
    //       util.setCode(code);
    //       wx.setStorageSync('code', code)
    //       that.setData({
    //         code
    //       })
    //         //没有用户信息
    //         wx.showModal({
    //           title: '授权',
    //           content: '小程序要取得您的微信头像和昵称，请点【确定】继续',
    //           complete: async (res) => {
    //             //取消
    //             if (res.cancel) {
      
    //             }
    //             //确定
    //             if (res.confirm) {
    //               //取得用户信息
    //               await that.getUserInfo();
    //               //登录服务器
    //               //that.loginServer();
    //               //跳转
    //               //  that.jumpToPage();
    //             }
    //           }
    //         })
    //     },
    //   })
    // }
  },
  async getUserInfo() {
    let that = this;
     wx.getUserProfile({
      desc: '运行联合宝宝',
      success: function (e) {
       // console.log("success,e", e)
        let userInfo = e.userInfo;
          //存到本面变量
          that.setData({
            nickname: userInfo.nickName,
            head_image: userInfo.avatarUrl
          })
        //存到本地缓存
        wx.setStorageSync('userInfo', userInfo)
        //存到全局变量
        const app = getApp();
        app.globalData.userInfo = userInfo;
        const util = require("../../utils/util")
        const api = require("../../api/api")
        let nickname = wx.getStorageSync('userInfo').nickName;
        let head_image = wx.getStorageSync('userInfo').avatarUrl;
        api.user.loginByWechatCode({
          code: that.data.code,
          channel: that.data.channel,
          hash: that.data.hash,
          nickname,
          head_image,
          success(res) {
            // console.log("afterServiceOrderItem,orderDetail,res", res)
            let data = res.data.data;
            if(res.data.code == 0){
              that.setData({
                token: data.token,
              })
              wx.setStorageSync('token', data.token);
              wx.setStorageSync('openid', data.openid);

            }
          }
        });
        // console.log("app.globalData", app.globalData)
        //跳转
        // that.jumpToPage();
      }
    })
  },
/**
 * 设置登录数据
 * @param {Object} data 
 */
 setLoginData(data) {
  const app = getApp();
  app.globalData.loginData = data;
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabbarChange(event){
    // console.log("daoQi,onTabbarChange,event",event)
    this.setData({
      active:event.detail
    })
  },
})